import { _decorator, Component, Node ,UITransform ,ScrollView ,Button ,Label,Prefab,instantiate,Vec3} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('gameManager')
export class gameManager extends Component {
    @property({ type: ScrollView })
    scrollView: ScrollView = null!; // 需要显示在圆角容器内的内容节点

    @property({ type: Prefab })
    listItem: Prefab = null!; // 获取预制体

    @property
    listItemNode: Node = null;

    @property({ type: Node })
    leftLayout: Node = null!; // 预制体节点绑定的父节点

    @property({ type: Node })
    rightLayout: Node = null!; // 预制体节点绑定的父节点

    @property 
    spacingY: number = 5; // 垂直间距

    @property
    columnheight = {
        1: 0, // 第一列的高度
        2: 0, // 第二列的高度
    }


    start() {
        ////https://cdn.133.cn/ticket/vue/promotion/2025xhsquanyi/pyq1.png
        // this.bg.spriteFrame = '';
        // this.loadNetworkImage();
        //将预制体转为节点
         
        for (let i = 0; i < 30; i++) {
            // console.log(this.listItem);
            this.listItemNode = instantiate(this.listItem);
            // console.log(this.listItemNode.getComponent(UITransform).height);
           if(i==1||i==4||i==7||i==10||i==13||i==16||i==19||i==22||i==25||i==28){
                this.listItemNode.getComponent(UITransform).height = 500;
            }
            // console.log(this.listItemNode.getComponent(UITransform).height);
            
            let button:any = this.listItemNode.getComponent(Button);
            button.node.on('click',()=>{
                console.log('click:',i);
            }, this);
            this.listItemNode.children[0].children[0].getComponent(Label).string = 'boonookleft'+i.toString();
            this.listItemNode.children[0].children[1].getComponent(Label).string = 'boonookright'+i.toString();
            
            // console.log(this.listItemNode.children().getComponent(Label));
            //将节点添加到contentNode中
            let oneHeight = this.listItemNode.getComponent(UITransform).height;
            if(i==0){
                this.listItemNode.parent = this.leftLayout;
                this.columnheight[1] += oneHeight + this.spacingY;
            }else if(i==1){
                this.listItemNode.parent = this.rightLayout;
                this.columnheight[2] += oneHeight + this.spacingY;
            }else{
                if(this.columnheight[1] < this.columnheight[2]){
                    this.listItemNode.parent = this.leftLayout;
                    this.columnheight[1] += oneHeight + this.spacingY; 
                }else{
                    this.listItemNode.parent = this.rightLayout;
                    this.columnheight[2] += oneHeight + this.spacingY;
                }
            }
            
            
            // if(this.columnheight[1] < this.columnheight[2]){
            //     this.columnheight[1] += oneHeight + this.spacingY;
            // }else{
            //     this.columnheight[2] += oneHeight + this.spacingY;
            // }
            // // var num = parseInt(prompt('请输入一个数字')); // 输入一个数字
            // if ((i+1) % 2 === 0) {
            //     // 偶数行，添加到第二列
            // } else {
            //     // 奇数行，添加到第一列
            // }
        }
        
    }

    onScrolling(scrollview, eventType, customEventData) {
        console.log('onScrolling:',scrollview, eventType, customEventData);
    }

    protected async onLoad() {
        // this.buyBtn.on(Button.EventType.CLICK,(event)=>{
        //     alert(231123)
        // }, this);
        // console.log(this.Bg.getComponent(Sprite));//spriteFrame
        // resources.load('https://cdn.133.cn/ticket/vue/promotion/2025xhsquanyi/pyq1.png',SpriteFrame, (err, texture) => { 
        //     if(err){
        //         console.log('err:',err);
        //         return;
        //     }
        //     this.Bg.getComponent(Sprite).spriteFrame = texture;
        // });
        
    }

    //  async loadNetworkImage() {
    //     try {
    //     let imageUrl = 'http://img.daimg.com/uploads/allimg/250504/3-250504224529.jpg';

    //     // 使用 fetch 获取图片
    //     const response = await fetch(imageUrl);
    //     const blob = await response.blob();
    //     const imgBitmap = await createImageBitmap(blob);
        
    //     // 创建 Texture2D
    //     const texture = new Texture2D();
    //     texture.uploadData(imgBitmap);

    //     // 创建 SpriteFrame
    //     const spriteFrame = new SpriteFrame();
    //     spriteFrame.texture = texture;
    //     this.Bg.getComponent(Sprite).spriteFrame = spriteFrame;
    //     } catch (error) {
    //         console.error('Error loading image:', error);
    //     }
    //  }

    update(deltaTime: number) {
        
    }
}


